package top.itaso.app.common;

import android.content.SharedPreferences;
import android.text.TextUtils;

import top.itaso.app.MyApplication;

import static android.content.Context.MODE_PRIVATE;

/**
 * @Description: SharedPreferences 存储工具类
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Gitee https://gitee.com/itsdf07
 * @Date 2022/1/27
 */
public class SPUtils {
    public static class SPKey {
        /**
         * 记住的账号
         */
        public static final String SP_KEY_USERNAME = "USERNAME";
        /**
         * 记住的账号对应的密码
         */
        public static final String SP_KEY_USERNAME_PASSWORD = "USERNAME_PASSWORD";

        /**
         * 记住是否开启功能保护：true-打开，即受保护功能不可用，false-关闭，即对所有开放
         */
        public static final String SP_KEY_PROTECT_STATUS = "PROTECT_STATUS";
        /**
         * 记住是否开启功能保护所设置的密码
         */
        public static final String SP_KEY_PROTECT_PWD = "PROTECT_PWD";

        /**
         * 当前是否 Debug 模式，false-非 debug （默认），true-是 debug 模式
         */
        public static final String SP_KEY_IS_DEBUG = "IS_DEBUG";
    }

    public static final String SHARED_PREFERENCES = "itaso";
    private SharedPreferences mSp;
    private static SPUtils mInstance;

    public static SPUtils getInstance() {
        if (mInstance == null) {
            synchronized (SPUtils.class) {
                if (mInstance == null) {
                    mInstance = new SPUtils();
                }
            }
        }
        return mInstance;
    }

    private SPUtils() {
        mSp = MyApplication.getContext().getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
    }

    /**
     * 保存基本数据类型的数据
     *
     * @param key
     * @param value
     */
    public void saveValue(String key, Object value) {
        SharedPreferences.Editor editor = mSp.edit();
        if (!TextUtils.isEmpty(key) && value != null) {
            if (value instanceof String) {
                editor.putString(key, (String) value);
            } else if (value instanceof Integer) {
                editor.putInt(key, (Integer) value);
            } else if (value instanceof Long) {
                editor.putLong(key, (Long) value);
            } else if (value instanceof Float) {
                editor.putFloat(key, (Float) value);
            } else if (value instanceof Boolean) {
                editor.putBoolean(key, (Boolean) value);
            }
        }
        editor.commit();
    }

    public <T> T getValue(String key, Class<T> clazz) {
        if (!TextUtils.isEmpty(key) && clazz != null) {
            String className = clazz.getSimpleName();
            if ("String".equals(className)) {
                return (T) mSp.getString(key, "");
            } else if ("Integer".equals(className)) {
                return (T) Integer.valueOf(mSp.getInt(key, 0));
            } else if ("Long".equals(className)) {
                return (T) Long.valueOf(mSp.getLong(key, 0));
            } else if ("Float".equals(className)) {
                return (T) Float.valueOf(mSp.getFloat(key, 0));
            } else if ("Boolean".equals(className)) {
                return (T) Boolean.valueOf(mSp.getBoolean(key, false));
            }
        }

        return null;
    }

    public <T> T getValue(String key, Class<T> clazz, Class<T> defaultValue) {
        if (!TextUtils.isEmpty(key) && clazz != null) {
            String className = clazz.getSimpleName();
            if ("String".equals(className)) {
                return (T) mSp.getString(key, String.valueOf(defaultValue));
            } else if ("Integer".equals(className)) {
                return (T) Integer.valueOf(mSp.getInt(key, Integer.valueOf(String.valueOf(defaultValue))));
            } else if ("Long".equals(className)) {
                return (T) Long.valueOf(mSp.getLong(key, 0));
            } else if ("Float".equals(className)) {
                return (T) Float.valueOf(mSp.getFloat(key, 0));
            } else if ("Boolean".equals(className)) {
                return (T) Boolean.valueOf(mSp.getBoolean(key, false));
            }
        }

        return null;
    }

    //--------------------------------------------------------------------------------------------------------------------------
    public void save(String key, String value) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString(key, value);
        editor.commit();

    }

    public void remove(String key) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.remove(key);
        editor.commit();
    }

    public String read(String key) {
        String info = mSp.getString(key, "");
        return info;
    }

    public void saveBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putBoolean(key, value);
        editor.commit();

    }

    public boolean readBoolean(String key, boolean defValue) {
        boolean info = mSp.getBoolean(key, defValue);
        return info;
    }

    //--------------------------------------------------------------------------------------------------------------------------
    public boolean readIsProtect() {
        String curPwdValue = getValue(SPUtils.SPKey.SP_KEY_PROTECT_PWD, String.class);
//        boolean info = readBoolean(SPKey.SP_KEY_PROTECT_STATUS, true);
        return !TextUtils.isEmpty(curPwdValue);
    }

    public boolean readIsDebug() {
        boolean info = readBoolean(SPKey.SP_KEY_IS_DEBUG, true);
        return info;
    }
}
